/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.live.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.enums.LiveInterfaceType;
import com.yami.shop.bean.model.Product;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.live.common.model.LiveProdStore;
import com.yami.shop.live.common.param.LiveUsableNumParam;
import com.yami.shop.live.common.service.LiveLogService;
import com.yami.shop.live.common.service.LiveProdStoreService;
import com.yami.shop.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author LGH
 * @date 2020-08-04 16:57:17
 */
@RestController
@AllArgsConstructor
@RequestMapping("/platform/live/liveProdStore")
@Api(tags = "平台端直播间商品库接口")
public class LiveProdStoreController {

    private final LiveProdStoreService liveProdStoreService;
    private final LiveLogService liveLogService;
    private final ProductService productService;

    @GetMapping("/page")
    @ApiOperation(value = "分页查找直播间商品库信息")
    public ServerResponseEntity<IPage<LiveProdStore>> getLiveProdStorePage(PageParam<LiveProdStore> page, LiveProdStore liveProdStore) {
        IPage<LiveProdStore> resPage = liveProdStoreService.getPage(page, liveProdStore);
        return ServerResponseEntity.success(resPage);
    }

    @GetMapping("/info/{liveProdStoreId}")
    @ApiOperation(value = "通过id查询直播间商品库")
    @ApiImplicitParam(name = "liveProdStoreId", value = "直播间商品库id", required = true, dataType = "Long")
    public ServerResponseEntity<LiveProdStore> getById(@PathVariable("liveProdStoreId") Long liveProdStoreId) {
        LiveProdStore liveProdStore = liveProdStoreService.getById(liveProdStoreId);
        if (Objects.nonNull(liveProdStore.getProdId())) {
            Product product = productService.getProductByProdId(liveProdStore.getProdId(), I18nMessage.getDbLang());
            if (product != null) {
                liveProdStore.setPic(product.getPic());
                liveProdStore.setProdName(product.getProdName());
                liveProdStore.setOriProdType(product.getProdType());
            }
        }
        return ServerResponseEntity.success(liveProdStore);
    }

    @DeleteMapping("/{liveProdStoreId}")
    @ApiOperation(value = "通过id删除直播间商品库")
    @ApiImplicitParam(name = "liveProdStoreId", value = "直播间商品库id", required = true, dataType = "Long")
    public ServerResponseEntity<Boolean> removeById(@PathVariable Long liveProdStoreId) throws WxErrorException {
        liveProdStoreService.removeWxLiveProdById(liveProdStoreId, Constant.PLATFORM_SHOP_ID);
        return ServerResponseEntity.success();
    }

    @GetMapping("/getAddProdNum")
    @ApiOperation(value = "查询商家今日剩余可用添加直播商品、可删除商品库商品次数")
    public ServerResponseEntity<LiveUsableNumParam> getAddRoomNum() {
        LiveUsableNumParam liveNum = liveLogService.getLiveNum(Constant.PLATFORM_SHOP_ID, LiveInterfaceType.ADD_PROD_AUDIT);
        LiveUsableNumParam deleteLiveNum = liveLogService.getLiveNum(Constant.PLATFORM_SHOP_ID, LiveInterfaceType.DELETE_PROD);
        liveNum.setDeleteShopNum(deleteLiveNum.getShopNum());
        liveNum.setDeleteTotalNum(deleteLiveNum.getTotalNum());
        return ServerResponseEntity.success(liveNum);
    }
}
